/** 瀑布流* */
$(function() {
	initData();
	
	$("#loadMeinvMOre").click(function() {
		$minUl = getMinUl();
		loadMeinv();
	});
	
	$(window).resize(function () {//当浏览器大小变化时
		$('.cover img').each(function(i , img){
			var w = $(this).width();
			$(this).attr('height',w);
		});
	});
});

function initData() {
	var mask = $.maskWeb();
	loadMeinv();
	$.unmaskWeb(mask);
}

function loadMeinv() {
	var loadimgPath = ctx + "/static/skins/images/imgloader.gif";
	var pageno = $("#loadMeinvMOre").attr("pageno");
	$("#loadMeinvMOre").html("数据加载中...");
	$.ajax({
		url : ctx + "/web/product/ajaxList",
		type : "post",
		dataType : "json",
		data : {pageno: pageno},
		async : false,
		success : function (result) {
			$.each(result.data.data, function (i, n) {
				var id = Math.uuid(10);
				var imgUrl = ctx + n.image1;
				var html = new Array();
				
				html.push('<li class="hproduct">');
				if(n.stock > 0){
					html.push('<div class="cover">');
					html.push('<a href="' + ctx + '/web/product/detail?id=' + n.id + '">');
					html.push('<img done="true" id="' + id + '" src="' + loadimgPath + '" style="display: inline;">');
					html.push('</a>');
					html.push('</div>');
					html.push('<p class="shopName_n">');
					html.push('<a href="' + ctx + '/web/product/detail?id=' + n.id + '">');
					html.push( n.productName);
					html.push('</a></p>');
				}else{
					html.push('<div class="cover">');
					html.push('<img done="true" id="' + id + '" src="' + loadimgPath + '" style="display: inline;">');
					html.push('</div>');
					html.push('<p class="shopName_n">');
					html.push('' + n.productName);
					html.push('</p>');
					html.push('<div class="nostock-bg"><div><span class="nostack">已售罄</span></div></div>');
				}
				html.push('<p class="shopPrice_n clear">');
				html.push('<strong class="red">￥' + changeTwoDecimal(n.salesPrice) + '</strong><span class="del">￥' + changeTwoDecimal(n.marketPrice) + '</span>');
				html.push('</p>');
				html.push('</li>');
				$("#shopListShow_new ul").append(html.join(''));
				loadImage(id, imgUrl, loadResult);
			});
			
			if (result.data.data.length >= 10) {
				$("#loadMeinvMOre").show();
			}
			
			if (result.data.data.length > 0) {
				$("#loadMeinvMOre").attr("pageno", parseInt(pageno) + 1);
				$("#loadMeinvMOre").html("加载更多");
			} else {
				$("#loadMeinvMOre").html("已经没有更多数据了");
			}
			
			$('.cover img').each(function(i , img){
				var w = $(this).width();
				$(this).attr('height',w);
			});
		}
	});
}

function getMinUl() {
	var $arrUl = $("#container .col");
	var $minUl = $arrUl.eq(0);
	$arrUl.each(function(index, elem) {
		if ($(elem).height() < $minUl.height()) {
			$minUl = $(elem);
		}
	});
	return $minUl;
}